package cn.xiu.drugstore.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.xiu.drugstore.pojo.Staff;
import cn.xiu.drugstore.service.StaffService;
import cn.xiu.drugstore.service.impl.StaffServiceImpl;

public class UserServlet extends HttpServlet {


	private static final long serialVersionUID = 1L;


	public void destroy() {
		super.destroy();
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		System.out.println("进来了阿斯顿发斯蒂芬");
		
		String name = request.getParameter("name");
		String pass=request.getParameter("password");
		//System.out.println("11111111111"+name);
		//System.out.println("11111111111"+pass);
		
		StaffService ss=new StaffServiceImpl();
		Staff staff = ss.findByName(name);
		//System.out.println("11111111111"+staff);
		String usertype=request.getParameter("clerk");
		System.out.println("11111111111"+usertype);
		int type=Integer.valueOf(usertype);
		if(staff!=null){
			//用户名存在
			if(staff.getStaffType()==type){
			//角色正确
			if(staff.getStaffPass().equals(pass)){
				//密码正确
				if(staff.getStaffType()==0){
					out.println("<script>alert('登录成功');location.href='index.jsp';</script>");
				}else{
					out.println("<script>alert('登录成功');location.href='cashier.jsp';</script>");
				}
			}else{
				//密码不正确
				out.println("<script>alert('用户密码不正确');location.href='login.jsp';</script>");
			}
			}else{
				//角色不正确
				out.println("<script>alert('用户角色不正确');location.href='login.jsp';</script>");
			}
		}else{
			//用户名不存在
			out.println("<script>alert('用户名不存在');location.href='login.jsp';</script>");
		}
		out.flush();
		out.close();
	}



	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

	
	public void init() throws ServletException {
		// Put your code here
	}

}
